﻿/*
 * Copyright 2009 José David Tuero Rodríguez

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace AtmosCreator
{
    /// <summary>
    /// Class used to select the language of the game
    /// </summary>
    public partial class FrmLanguage : Form
    {

        /// <summary>
        /// Form father of this
        /// </summary>
        private Form _father;


        /// <summary>
        /// Localizer
        /// </summary>
        Localizer _localizer;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="father">The father form of this</param>
        /// <param name="localizer">Localizer</param>
        public FrmLanguage(Form father, Localizer localizer)
        {
            _father = father;
            _localizer = localizer;
            InitializeComponent();
            localizer.LocalizeForm(this);
            string[] languages = null;
            try
            {
                languages = Directory.GetFiles("localize", "*.xml");
                
            }
            catch
            {
                MessageBox.Show(localizer.GetError("LocalizeFolderError"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Application.Exit();
            }
            char[] separators = { '\\' };
            for (int i = 0; i < languages.Length; i++)
            {
                string[] tem = languages[i].Split(separators);
                tem[1]=tem[1].Remove(tem[1].Length - 4);
                cmbLanguage.Items.Add(tem[1].ToLower());
                if (languages[i] == _localizer.File)
                {
                    cmbLanguage.SelectedIndex = i;
                }
            }
        }


        /// <summary>
        /// Make the father form visible
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void FrmLanguage_FormClosed(object sender, FormClosedEventArgs e)
        {
            _father.Visible = true;
        }

        /// <summary>
        /// Save the settings
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Localizer localizer = new Localizer("localize\\" +cmbLanguage.Items[cmbLanguage.SelectedIndex] + ".xml");
                ((FrmMain)_father).SetLocalizer(localizer);
                LanguageControl language;
                try
                {
                    language = new LanguageControl();
                    language.Language = cmbLanguage.Items[cmbLanguage.SelectedIndex].ToString();
                    try
                    {
                        language.Save();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show(_localizer.GetError("LanguageFileSaveError"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        Application.Exit();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show(localizer.GetError("LanguageFileError"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Application.Exit();
                }
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Could not find the language file in the hard disk or the file is corrupt." + Environment.NewLine + "AtmosCreator will close", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            this.Close();
        }


    
    }
}
